package br.ita.doacoes.view.templates;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.swingBean.actions.ApplicationAction;
import org.swingBean.descriptor.GenericFieldDescriptor;
import org.swingBean.descriptor.XMLDescriptorFactory;
import org.swingBean.gui.JActButton;
import org.swingBean.gui.JBeanPanel;

import br.ita.doacoes.core.templates.DAO;
import br.ita.doacoes.view.cadastrodoacoes.refresher.Refresher;

public class ConsultaDetalheSimples extends JPanel {
	
	protected JBeanPanel panelFormulario;
	protected JActButton botaoUpdate;
	protected JActButton botaoSair;
	protected JPanel panelButton;
	
	private String xmlForm;
	private String formName;
	private Class classe;
	private DAO dao;
	private JFrame frame;
	
	protected Object currentOnForm;

	public ConsultaDetalheSimples(JFrame frame) {
		super();
		this.frame = frame;
	}
	
	public ConsultaDetalheSimples() {
		super();
	}

	public DAO getDao() {
		return dao;
	}

	public void setDao(DAO dao) {
		this.dao = dao;
	}

	public Class getClasse() {
		return classe;
	}

	public void setClasse(Class classe) {
		this.classe = classe;
	}

	public String getFormName() {
		return formName;
	}

	public void setFormName(String formName) {
		this.formName = formName;
	}

	public String getXmlForm() {
		return xmlForm;
	}

	public void setXmlForm(String xmlForm) {
		this.xmlForm = xmlForm;
	}

	public void init(Object o){
		
		o = getDao().getById(o);
		initComponents();
		createButtons(o);
		createButtonPanel();
		putComponentsInPanel();
		panelFormulario.setBean(o);
	}

	protected void putComponentsInPanel() {
		setLayout(new BorderLayout());
		add(panelFormulario, BorderLayout.NORTH);
		add(panelButton, BorderLayout.SOUTH);
	}

	protected void initComponents() {
		GenericFieldDescriptor formdescriptor = XMLDescriptorFactory
				.getFieldDescriptor(classe, xmlForm, formName);
		panelFormulario = new JBeanPanel(classe, formdescriptor);
	}

	protected void createButtons(final Object o) {

		botaoUpdate = new JActButton("Salvar",
						new ApplicationAction() {
							public void execute() {
								panelFormulario.populateBean(o);
								dao.update(o);
								Refresher.refresh();
							}
						});
	
		botaoSair = new JActButton("Sair",
						new ApplicationAction() {
							public void execute() {
								frame.dispose();
							}
						});

	}

	protected void createButtonPanel() {
		panelButton = new JPanel();
		panelButton.add(botaoUpdate);
		panelButton.add(botaoSair);
	}

	public JBeanPanel getPanelFormulario() {
		return panelFormulario;
	}

	public void setPanelFormulario(JBeanPanel panelFormulario) {
		this.panelFormulario = panelFormulario;
	}

	public JPanel getPanelButton() {
		return panelButton;
	}

	public void setPanelButton(JPanel panelButton) {
		this.panelButton = panelButton;
	}

	public JFrame getFrame() {
		return frame;
	}

	public void setFrame(JFrame frame) {
		this.frame = frame;
	}

}
